﻿using LettuceEncrypt.Acme;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;

namespace LettuceEncrypt.Extensions.AliDns;

public static class DenpendencyInjectionExtensions
{
    public static ILettuceEncryptServiceBuilder AddAliDns(this ILettuceEncryptServiceBuilder builder)
    {
        builder.Services.AddTransient<IConfigureOptions<AlibabaCloudOptions>>(sp =>
        {
            var config = sp.GetService<IConfiguration?>();
            return new ConfigureOptions<AlibabaCloudOptions>(options => config?.Bind("AlibabaCloud", options));
        });

        builder.Services.Replace(ServiceDescriptor.Singleton(typeof(IDnsChallengeProvider), typeof(AliDns01ChallengeProvider)));
        builder.Services.AddSingleton<AlibabaCloudDnsClient>();

        return builder;
    }
}
